//
//  ECNewPasswordViewController.m
//  Edu_iOS
//
//  Created by apple on 2017/9/11.
//  Copyright © 2017年 yanyan. All rights reserved.
//


#define  kHeight_InputTextField        100.f/2         //输入框的高度
#import "ECRegularExpression.h"

#import "ECNewPasswordViewController.h"
#import "ECOthersLoginViewController.h"

#import "ECUITextField_Account.h"

#import "UIImage+Color.h"

#import "CYDataHelper.h"

@interface ECNewPasswordViewController (){
    ECUITextField_Account *passwordArea;
    ECUITextField_Account *makeSurePasswordArea;
}

@end

@implementation ECNewPasswordViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    
    self.title = @"忘记密码";

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"navigation_Return"] style:UIBarButtonItemStylePlain target:self action:@selector(action_dismiss)];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:19],NSForegroundColorAttributeName:[UIColor colorWithRed:0.13f green:0.13f blue:0.13f alpha:1.00f]}];
    
    /*  导航栏线  */
    [self.navigationController.navigationBar setShadowImage:[UIImage imageWithColor:[UIColor colorWithHexString:@"#BFBFBF"] size:CGSizeMake(self.view.frame.size.width, 0.5)]];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    passwordArea = [[ECUITextField_Account alloc]initWithFrame:CGRectMake(kProportion_Width(MarginLeft_Textfield), kProportion_Width(60) + kNavHeight, kScreenWidth - kProportion_Width(60*2),  kHeight_InputTextField) placeholder:@"登录密码(至少6个字符)" leftImage:[UIImage imageNamed:@"pwd_user"]];
    passwordArea.isSecureTextEntry = YES;
    [self.view addSubview:passwordArea];
    
    makeSurePasswordArea = [[ECUITextField_Account alloc]initWithFrame:CGRectMake(kProportion_Width(MarginLeft_Textfield), CGRectGetMaxY(passwordArea.frame) + kProportion_Width(60), kScreenWidth - kProportion_Width(60*2),  kHeight_InputTextField) placeholder:@"新密码" leftImage:[UIImage imageNamed:@"pwd_user"]];
    makeSurePasswordArea.isSecureTextEntry = YES;
    [self.view addSubview:makeSurePasswordArea];
    
    ECUButton *makesureBtn = [ECUButton buttonWithType:UIButtonTypeCustom];
    makesureBtn.frame = CGRectMake(kProportion_Width(60), CGRectGetMaxY(makeSurePasswordArea.frame) + kProportion_Width(MarginHeight_textfield), kScreenWidth - kProportion_Width(60*2),(kScreenWidth - kProportion_Width(60*2))* 0.1655);
    [self.view addSubview:makesureBtn];
    [makesureBtn setTitle:@"确定" forState:0];
    [makesureBtn setTitleColor:[UIColor colorWithRed:255.f/255.f green:103.f/255.f blue:0 alpha:1.00f] forState:0];
    [makesureBtn setBackgroundImage:[UIImage imageNamed:@"flatButton_colorNormal"] forState:UIControlStateNormal];
    makesureBtn.titleLabel.font = [UIFont systemFontOfSize:16*kPercentage];
    [makesureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [makesureBtn addTarget:self action:@selector(makesureBtnClick) forControlEvents:UIControlEventTouchUpInside];
}

-(void)makesureBtnClick{

    if (([CYDataHelper isNull:passwordArea.textField.text] == NO) && [passwordArea.textField.text isEqualToString:makeSurePasswordArea.textField.text]) {
        if (![ECRegularExpression detectionIsPasswordQualified:passwordArea.textField.text]) {
            [MBProgressHUD showTipMessageInWindow:kAlertTipOfPasswordValid];
            return;
        }
        
        [HWHttpTool requestWithMethod:@"POST"
                                  url:URL_POST_ResetPwd
                               params:@{@"mobile":self.telPhone,
                                      @"password":passwordArea.textField.text,
                                        }
                              headers:nil
                              success:^(id json) {
                                  [MBProgressHUD showTipMessageInWindow:@"密码设置成功"];
                                  [self dismissViewControllerAnimated:YES completion:^{
                                  }];
                              }
                              failure:^(NSError *error) {
                                  NSString *str = [ECData_Json requestErrorTipWith:error];
                                  DLog(@"%@",str);
                                  if (!([str isEqualToString:@"101"] || [str isEqualToString:@"401"])) {
                                      [MBProgressHUD showTipMessageInWindow:str];
                                  }
                              }];
    }else{
        [MBProgressHUD showTipMessageInWindow:@"两次密码不一致"];
    }
    
}

-(void)action_dismiss{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
